package win.smartown.kotlin.`class`

/**
 * Created by smartown on 2017/6/7.
 */
//在kotlin中类可以有属性，使用var声明可变属性，val声明只读属性
public class Address {
    public var name: String = ""
    public var street: String = ""
    public var city: String = ""
    public var state: String = ""
    public var zip: String = ""
}

fun copyAddress(address: Address): Address {
    val result = Address()
    result.city = address.city
    result.name = address.name
    return result
}

//Getter和Setter
//声明一个属性的完整语法如下：
//var <propertyName> : <propertyType> [ = <property_initializer>]
//        <getter>
//        <setter>
//语法中的初始化语句，getter和setter都是可选的。
//如果属性类型可以从初始化语句或者类的成员函数中推断出来，那么它的类型声明也是可选的。
var allByDefault: Int? = 0 //需要初始化语句，默认实现了setter和getter
var initialized = true //类型为Boolean，默认实现了setter和getter

//只读属性的声明语法和可变属性的声明语法相比有亮点不同：
//它以val而不是var开头、不允许setter函数：
class Simple constructor(i: Int) {
    //类型为Int，默认实现getter，单必须在构造函数中初始化
    val simple: Int?

    init {
        simple = i
    }
}

val simple = 1 //类型为Int，默认实现getter

class GetterSample {

    var size = 1

    var isEmpty: Boolean = false
        get() = this.size == 0

    var stringRepresentation: String
        get() = this.toString()
        set(value) {}

}